using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Boss : MonoBehaviour
{
    private int maxHealth = 50;
    private int curHealth = 0;
    
    public bool is_Die = false;

    public GameObject player;

    public float speed = 1.5f;

    private Animator anim;

    public GameUI gameUI;

    public Transform[] tr;

    private bool is_boom = false;
    private bool is_fire = false;
    public GameObject Boom1;
    public GameObject bullet1;

    private float dis;

    private void Start()
    {
        Soundmgr.Instance.PlayMusicByName("boss战",Camera.main.transform.position,0.2f);
        curHealth = maxHealth;
        anim = GetComponent<Animator>();
        player = GameObject.FindGameObjectWithTag("Player");
        gameUI = GameObject.Find("Canvas").GetComponent<GameUI>();
        gameUI.bosshealthBar.value = curHealth;
        gameUI.bosshealthBar.gameObject.SetActive(true);
    }

    private void Update()
    {
        if(player != null)
        {
            dis = Mathf.Abs(transform.position.x - player.transform.position.x);
            Move();
            if (dis < 0.5f)
            {
                Skill1();
            }
            else
            {
                Skill2();
            }
        }
        else
        {
            player = GameObject.FindGameObjectWithTag("Player");
        }
    }

    public bool Move()
    {

        if(dis > 0.1f)
        {
            float x = Mathf.MoveTowards(transform.position.x, player.transform.position.x, Time.deltaTime * speed);
            transform.position = new Vector3(x, transform.position.y, 0);
            return true;
        }
        return false;
    }

    public void Hurt(int dam)
    {
        curHealth-=dam;
        gameUI.UpdatebossSlider(curHealth);
        if (curHealth <= 0)
        {
            anim.SetTrigger("die");
            gameUI.bosshealthBar.gameObject.SetActive(false);
        }
    }

    public void Die()
    {
        Soundmgr.Instance.PlayMusicByName("jetExplosion",transform.position,0.7f);
        gameUI.ShowComplete();
        Destroy(gameObject);
    }
    
    public void Skill1()
    {
        if(!is_boom)
        {
            GameObject.Instantiate(Boom1, tr[0].position,Quaternion.identity);
            is_boom = true;
            StartCoroutine(boominterval());
        }
    }

    private IEnumerator boominterval()
    {
        yield return new WaitForSeconds(3.5f);
        is_boom = false;
    }

    public void Skill2()
    {
        if (!is_fire)
        {
            GameObject.Instantiate(bullet1, tr[1].position, Quaternion.identity);
            GameObject.Instantiate(bullet1, tr[2].position, Quaternion.identity);
            is_fire = true;
            StartCoroutine (fireinterval());
        }
    }

    private IEnumerator fireinterval()
    {
        yield return new WaitForSeconds(3.5f);
        is_fire = false;
    }

}
